package day12;

import java.io.File;
import java.util.Scanner;

public class RecursiveDeleteEmptyFoldersWithInput {
    public static void deleteEmptyFolders(File dir) {
        File[] subFiles = dir.listFiles();
        if (subFiles!= null) {
            for (File file : subFiles) {
                if (file.isDirectory()) {
                    deleteEmptyFolders(file);  // 递归处理子文件夹
                    if (file.list().length == 0) {
                        // 如果子文件夹为空，则删除它
                        boolean deleted = file.delete();
                        if (deleted) {
                            System.out.println("已成功删除空文件夹: " + file.getAbsolutePath());
                        } else {
                            System.out.println("删除空文件夹失败: " + file.getAbsolutePath());
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入根目录的路径（用于递归删除其及子目录下所有空文件夹）: ");
        String rootPath = scanner.nextLine();  // 获取用户输入的根目录路径

        File rootDir = new File(rootPath);
        if (rootDir.exists() && rootDir.isDirectory()) {
            deleteEmptyFolders(rootDir);
        } else {
            System.out.println("指定的根目录不存在或不是有效的目录");
        }

        scanner.close();
    }
}